class Animal {
    public String name;
    public int age;
    public Animal(String name, int age ) {
        this.name = name;
        this.age = age;
    }
    public void eat() {
        System.out.println(name + "吃食物");
    }
}

class Cat extends Animals {
    public Cat(String name, int age) {
        super(name, age);
    }
    @Override
    public void eat() {
        System.out.println(name + "吃食物");
    }
}

class Dog extends Animals {

    public Dog(String name, int age) {
        super(name, age);
    }
    @Override
    public void eat() {
        System.out.println(name + "吃食物");//与父类的eat方法构成重写
    }
}

public class TestAniamls {
    // 方法传参：形参为父类型引用，可以接收任意子类的对象
    public static void eat(Animals animals) {
        animals.eat();
    }
    //作返回值：返回任意子类对象
    public static Animals byAnimals(String var) {
        if ("小狗狗".equals(var)) {
            return new Dog("小狗狗", 1);
        } else if ("小猫猫".equals(var)) {
            return new Cat("小猫猫", 1);
        }else {
            return null;
        }
    }
    public static void main(String[] args) {
        //直接赋值：子类对象赋值给父类对象
        Animals cat = new Cat("小黑", 7);
        Dog dog = new Dog("大黄", 6);
        eat(cat);
        eat(dog);

    }
}
